import * as Cookies from "js-cookie"
const createLoginService = () => import('~/services/management-service/login.service')


export async function authInit({ store }) {

  const uid = Cookies.get('uid')
  if (!uid) return false

  const { LoginService } = await createLoginService()
  const loginService = new LoginService()

  const user: any = await new Promise((resolve, reject) => {
    loginService.loginByUserName(uid).subscribe(resolve, reject)
  }).catch(() => false)

  if (!user) return false

  if (user.systemStatus === 'DISABLED' && user.userName !== '01059519' && user.userName !== 'superadmin') {
    window.alert("目前系统暂停使用")
    return false
  }
  // 更新登录数据
  store.commit('updateUserToken', user.token);
  return true
}

export function removeUID({ store }) {
  store.commit('updateUserToken', "");
  Cookies.remove("uid")
}